{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "from selenium.webdriver.support import expected_conditions as EC\r\n",
    "from selenium.webdriver.support.wait import WebDriverWait\r\n",
    "from selenium.webdriver.common.by import By\r\n",
    "from selenium import webdriver\r\n",
    "from datetime import datetime\r\n",
    "import csv, os, time\r\n",
    "save_name = '2021-08-07'\r\n",
    "os.makedirs(save_name, exist_ok=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "driver = webdriver.Edge()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "driver.implicitly_wait(3)  # seconds\r\n",
    "driver.set_page_load_timeout(20)  # 设置页面加载超时\r\n",
    "driver.set_script_timeout(20)  # 设置页面异步js执行超时"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "driver.get(\"https://user.qzone.qq.com/<her qq number>\")\r\n",
    "# 登录 并 切换到说说。"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "WebDriverWait(driver,5).until(lambda x: x.find_element_by_id('app_canvas_frame'))"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "driver.switch_to.frame('app_canvas_frame')"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "def parse_shuoshuo(e):\r\n",
    "    md = e.find_element_by_class_name(\"md\")\r\n",
    "    imgs = md.find_elements_by_class_name('img_thumb')\r\n",
    "    res = {\r\n",
    "        'quote': e.find_element_by_css_selector(\"div.box.bgr3 > div.bd > pre\").text,\r\n",
    "        # 'datetime': datetime.strptime(e.find_element_by_css_selector('div.box.bgr3 > div.ft > div.info > span > a').get_attribute('title'), \"%Y年%m月%d日 %H:%M\"),\r\n",
    "        'datetime': e.find_element_by_css_selector('div.box.bgr3 > div.ft > div.info > span > a').get_attribute('title'),\r\n",
    "        'has_img': len(imgs) > 0\r\n",
    "        # 因为图像数量不确定，所以仅用此参数表示\r\n",
    "    }\r\n",
    "    res['is_rt'] = 'rt_content' in md.get_attribute(\"class\")\r\n",
    "    if res['is_rt']:\r\n",
    "        res['rt_by'] = md.find_element_by_css_selector('div.bd > a').text\r\n",
    "        res['rt_content'] = md.find_element_by_css_selector('div.bd > pre').text\r\n",
    "    else:\r\n",
    "        res['rt_by'], res['rt_content'] = '', ''\r\n",
    "    return res"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "for i in range(3):\r\n",
    "    time.sleep(5)\r\n",
    "    current = WebDriverWait(driver,5).until(lambda x: x.find_element_by_css_selector('p.mod_pagenav_main > span.current'))\r\n",
    "    np = WebDriverWait(driver,5).until(lambda x: x.find_element_by_css_selector('p.mod_pagenav_main > a:last-child'))\r\n",
    "    filename = os.path.join(save_name, current.text + '.csv')\r\n",
    "    print(filename)\r\n",
    "\r\n",
    "    try:\r\n",
    "        lis = WebDriverWait(driver,5).until(lambda x: x.find_elements_by_class_name('feed'))\r\n",
    "    except:\r\n",
    "        print(filename + 'failed')\r\n",
    "        np.click()\r\n",
    "        continue\r\n",
    "\r\n",
    "    with open(filename, 'w', newline='', encoding='utf-8') as f:\r\n",
    "        fieldnames = ['quote', 'datetime', 'has_img', 'is_rt', 'rt_by', 'rt_content']\r\n",
    "        writer = csv.DictWriter(f, fieldnames=fieldnames)\r\n",
    "        writer.writeheader()\r\n",
    "        for e in lis:\r\n",
    "            writer.writerow(parse_shuoshuo(e))\r\n",
    "    np.click()"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.9.6",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.6 64-bit (system)"
  },
  "interpreter": {
   "hash": "a68daae51d4dd913c3d7a2d718562c5d32bde3fd8fdc1e6a49887db3c8670163"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}